﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QLChoDiaOc.Process;

namespace QLChoDiaOc.Forms
{
    /// <summary>
    /// Interaction logic for WinDangNhap.xaml
    /// </summary>
    public partial class WinDangNhap : Window
    {
        SelectInfo _selectInfo = new SelectInfo();

        public WinDangNhap()
        {
            InitializeComponent();
        }

        private bool KiemTraUser(string username)
        {
            if (matKhauPasswordBox.Password.Equals("admin") && tenDangNhapTextbox.Text.Equals("admin"))
                return true;
            if (_selectInfo.SelectMatKhau(username) != "-1")
            {
                if (_selectInfo.SelectMatKhau(username) == matKhauPasswordBox.Password)
                    return true;
                else
                    return false;
            }
            else
                return false;
        }

        private void btnDangNhap_Click(object sender, RoutedEventArgs e)
        {
            if (tenDangNhapTextbox.Text == "" || matKhauPasswordBox.Password == "")
                MessageBox.Show("Phải nhập đầy đủ thông tin trước khi đăng nhập", "Thông báo");
            else
            {
                if (KiemTraUser(tenDangNhapTextbox.Text))
                {
                    Main temp = new Main(tenDangNhapTextbox.Text);
                    temp.Show();
                    this.Close();
                }
                else
                    MessageBox.Show("Kiểm Tra Lại Mật Khẩu hoặc Tên Đăng Nhập", "Thông Báo");
            }
        }

        private void tenDangNhapTextbox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                btnDangNhap_Click(sender, e);
        }

        private void matKhauPasswordBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                btnDangNhap_Click(sender, e);
        }

        private void btnThoat_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
